//
//  JMDropCicleView.m
//  VoluntaryStar
//
//  Created by xiaorui on 15/9/9.
//  Copyright (c) 2015年 ios. All rights reserved.
//

#import "JMDropCicleView.h"
#import "JMHeader.h"
#import "UIViewExt.h"


@interface JMDropCicleView (){
    NSArray *array1;
    NSArray *array2;
    
    UIButton *currBtn1;
    UIButton *currBtn2;
    NSString * gradeString;
    NSString * xuekeString;

    
}
@property (nonatomic, strong) UIView *pointToView; // the start position
@property (nonatomic, strong) UIView *backGroundView;

@property (nonatomic, strong) UIView *centView;

@property (nonatomic, strong) UIView *bottomView;

@property (nonatomic, strong) UIButton *submitBtn;

@end

@implementation JMDropCicleView

- (UIView *)centView
{
    if (!_centView) {
        _centView = [[UIView alloc]init];
        _centView.backgroundColor = [UIColor whiteColor];
        _centView.layer.masksToBounds =YES;
        _centView.layer.borderWidth= 0.5;
        _centView.userInteractionEnabled =YES;
        _centView.layer.borderColor = [UIColorFromRGB(0xc4c4c4)CGColor];
    }
    return _centView;
}

- (UIView *)bottomView
{
    if (!_bottomView) {
        _bottomView = [[UIView alloc]init];
        _bottomView.backgroundColor = [UIColor whiteColor];
        _bottomView.layer.masksToBounds =YES;
        _bottomView.layer.borderWidth= 0.5;
        _bottomView.userInteractionEnabled =YES;
        _bottomView.layer.borderColor = [UIColorFromRGB(0xc4c4c4)CGColor];
    }
    return _bottomView;
}

- (UIButton *)submitBtn
{
    if (!_submitBtn) {
        UIButton *button = [[UIButton alloc]init];
        [button setTitle:@"确定" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(submitButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [button setBackgroundColor:UIColorFromRGB(0xeb6100)];
        _submitBtn = button;
    }
    return _submitBtn;
}

- (instancetype)initWithFrame:(CGRect)frame
      contentHeight:(float) contentHeight{
    self = [super initWithFrame:frame];
    if (self) {
        self.userInteractionEnabled =YES;
        array1 = @[@"高一",@"高二",@"高三"];
        array2 = @[@"语文",@"数学",@"英语",@"历史",@"生物",@"物理",@"化学",@"政治",@"地理"];
        self.backgroundColor = [UIColor clearColor];
        UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WITH, UI_SCREEN_HEIGHT)];
        bgView.backgroundColor = [UIColor blackColor];
        bgView.alpha = 0.5;
        bgView.userInteractionEnabled =YES;
        [bgView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bgTapped:)]];
        [self addSubview:bgView];
        
        self.backGroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frameWidth, contentHeight)];
        self.backGroundView.backgroundColor = UIColorFromRGB(0xf3f3f3);
        [self addSubview:self.backGroundView];
        self.backGroundView.userInteractionEnabled =YES;
        self.backGroundView.transform = CGAffineTransformMakeTranslation(0,-contentHeight);
        
        
        UILabel *la = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 120, 30)];
        la.text =@"选择年级:";
        la.textColor = UIColorFromRGB(0x757575);
        la.font = [UIFont systemFontOfSize:14.0];
        [self.backGroundView addSubview:la];
        self.centView.frame = CGRectMake(-0.5, la.frameBottom, UI_SCREEN_WITH + 1, 60);
        float with = (UI_SCREEN_WITH - 120)/3.0;
        for (int i =0; i<array1.count; i++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(40 + (with + 20) *i, 5, with, 35);
            [btn setTitle:array1[i] forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
            [btn setTitleColor:UIColorFromRGB(0x757575) forState:UIControlStateNormal];
            [self.centView addSubview:btn];
        }
        [self.backGroundView addSubview:self.centView];
        
        
        UILabel *la2 = [[UILabel alloc]initWithFrame:CGRectMake(10,self.centView.frameBottom, 120, 30)];
        la2.text =@"选择科目:";
        la2.textColor = UIColorFromRGB(0x757575);
        la2.font = [UIFont systemFontOfSize:14.0];
        [self.backGroundView addSubview:la2];
        
        self.bottomView.frame = CGRectMake(-0.5, la2.frameBottom, UI_SCREEN_WITH +1, 100);
        [self.backGroundView addSubview:self.bottomView];
        float with2 = (UI_SCREEN_WITH - 135)/6.0;

        for (int i =0; i<array2.count; i++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(30 + (with2 + 15) *(i%6), 5 + (35 + 5) * (i/6), with2, 35);
            [btn setTitle:array2[i] forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(buttonClick2:) forControlEvents:UIControlEventTouchUpInside];
            btn.titleLabel.font = [UIFont systemFontOfSize:14.0];
            [btn setTitleColor:UIColorFromRGB(0x757575) forState:UIControlStateNormal];
            [self.bottomView addSubview:btn];
        }
        self.submitBtn.frame = CGRectMake(20, self.bottomView.frameBottom + 15, UI_SCREEN_WITH - 40, 40);
        [self.backGroundView addSubview:self.submitBtn];

        self.alpha = 0;
    }
    return self;
}

-(void)buttonClick:(UIButton *)sender{
    [currBtn1 setTitleColor:UIColorFromRGB(0x757575) forState:UIControlStateNormal];
    [sender setTitleColor:UIColorFromRGB(0xeb6100) forState:UIControlStateNormal];
    currBtn1 = sender;
    gradeString = [sender currentTitle];
}
-(void)buttonClick2:(UIButton *)sender{
    [currBtn2 setTitleColor:UIColorFromRGB(0x757575) forState:UIControlStateNormal];
    [sender setTitleColor:UIColorFromRGB(0xeb6100) forState:UIControlStateNormal];
    currBtn2 = sender;
    xuekeString = [sender currentTitle];
}

- (void)showDataPointToView:(UIView *)view SelectedItem:(SelectedItem)item diss:(DisMiss)diss{
    
    self.completeBlock = item;
    self.diss = diss;
   
    if(self.pointToView != view){
        [UIView transitionWithView:self.backGroundView
                        duration:0.35
                        options:UIViewAnimationOptionCurveLinear
                        animations:^{
                            self.alpha = 1;
                            self.backGroundView.transform = CGAffineTransformMakeTranslation(0,0);
                    }
            completion:NULL];
    self.pointToView = view;
    }
}

-(void)submitButtonClick:(UIButton *)sender{
    self.completeBlock(gradeString,xuekeString,self);
    [self hide];
}


- (void)bgTapped:(UITapGestureRecognizer *)tap
{
//    [self hide];
//    if (_diss) {
//        self.diss(self);
//    }
}

- (void)hide
{
    
    [UIView transitionWithView:self.backGroundView duration:0.35 options:UIViewAnimationOptionCurveLinear animations:^{
        self.backGroundView.transform = CGAffineTransformMakeTranslation(0,-self.backGroundView.frameHeight);
    } completion:^(BOOL finished) {
        
        self.alpha = 0;
    }];
    self.pointToView = nil;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
